package com.fc.push.handler.pub.verify;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.fc.commons.IDGenerator;
import com.fc.commons.UUIDUtil;
import com.fc.persistence.cache.ConstKey;
import com.fc.persistence.cache.redis.SharedJedisFacade;
import com.fc.persistence.dao.DAOTemplate;
import com.fc.push.GlobalServerConst;
import com.fc.push.db.TranscationFacade;
import com.fc.push.db.entities.Application;
import com.fc.push.handler.AbstractHttpHandler;
import com.fc.push.handler.HandlerResult;
import com.fc.push.handler.pub.MessageRCode;
import com.fc.remoting.anno.Protocol;

@Component
@Protocol(module = VerifyConst.VERIFY_MODULE_ID, order = VerifyConst.ORDER_1)
public class ApplicationRegisterHandler extends AbstractHttpHandler {
	private static final String PLATFORM = "platform";
	private static final String APPKEY = "appkey";
	private static final String APPNAME = "appname";
	private static final String PNAME = "pname";
	private static final String PKCSFILE = "pkcsfile";
	private static final String PWD = "pwd";
	private static final String IS_PRODUCTION = "isproduction";
	private static final String CODE = "code";
	@Override
	public Object handle(HttpServletResponse response, JSONObject request){
		String platform = request.getString(PLATFORM);
		String appname = request.getString(APPNAME);
		int isProduction = request.getIntValue(IS_PRODUCTION);
		if (platform == null || platform.isEmpty()) {
			JSONObject object = HandlerResult.result();
			object.put(CODE, MessageRCode.RCODE_ERROR_1002);
			return object;
		}
		platform = platform.toLowerCase();
		
		if (appname == null || appname.isEmpty()) {
			JSONObject object = HandlerResult.result();
			object.put(CODE, MessageRCode.RCODE_ERROR_1002);
			return object;
		}
		if(!appname.contains(platform)){
			appname = appname + "_" + platform;
		}
		
		String pName = null;
		byte[] pkcsfile = null;
		String pwd = null;
		String sql = "select app from Application app where appname = '" + appname
				+ "' and platform = '" + platform + "'";
		List<DAOTemplate> resApp = TranscationFacade.getInstance().transcation().getBySQL(sql);
		if (resApp != null && resApp.size() > 0) {
			JSONObject object = HandlerResult.result();
			object.put(CODE, MessageRCode.RCODE_ERROR_1004);
			return object;
		}
		
		if (platform.equals(GlobalServerConst.IOS)) {
			pkcsfile = request.getBytes(PKCSFILE);
			pwd = request.getString(PWD);
		} else if (platform.equals(GlobalServerConst.ANDROID)) {
			pName = request.getString(PNAME);
		}
		
		Application application = new Application();
		application.setAppname(appname);
		application.setId(IDGenerator.INSTANCE.generateId());
		application.setCreatedate(new Date());
		application.setPlatform(platform);
		application.setPname(pName);
		String uuid = UUIDUtil.INSTANCE.generatorUUID();
		application.setAppkey(uuid);
		application.setFile(pkcsfile);
		application.setPwd(pwd);
		application.setIsproduction(isProduction);
		TranscationFacade.getInstance().transcation().save(application);
		//保存app列表
		SharedJedisFacade.saddProxy(ConstKey.APP_SET, uuid);
		
		JSONObject result = HandlerResult.result();
		result.put(APPKEY, uuid);
		return result;
	}
}
